import { defineStore } from 'pinia'

export const useTagStore = defineStore('tagId', {
    state: () => {
        return {
            tagList: [
                { path: '/dashboard', name: '仪表盘' }, // 初始化默认显示首页
            ] as any
        }
    },
    getters: {},
    actions: {
        // 添加标签
        addTag(route) {
            let pathIndex = this.tagList.findIndex(item => item.path)
            let target = this.tagList.find(item => item.path == route.path)
            let isName = route.name
            // 跳转的路径没有添加过标签，并且有name
            if (!target && isName) {
                // 如果tagList是空的，直接push
                if (pathIndex == -1) {
                    this.tagList.push({ path: route.path, name: route.name })
                } else {
                    // 在第一个标签之后插入新元素
                    this.tagList.splice(pathIndex + 1, 0, { path: route.path, name: route.name })
                }
            }

        },
        // 删除标签
        delTag(route) {
            if(this.tagList.length==1) return
            this.tagList.forEach((item, index) => {
                if (item.path == route.path) {
                    this.tagList.splice(index, 1)
                }
            })
            // const index = this.tagList.findIndex(item => item.path === route.path)
            // if (index !== -1) {
            //     this.tagList.splice(index, 1)
            // }
        }
    }
})